<template>
  <div class="mainpg">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="main-controls-wrap">
          <el-button type="primary" @click="(addedFormVisible = true), (type = 0)">新增</el-button>
        </div>
        <div class="main_table">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
            :height="borderHeight"
            :header-cell-style="tableHeightStyle"
          >
            <el-table-column label="序号">
              <template slot-scope="scope">{{ scope.$index + 1 }}</template>
            </el-table-column>
            <el-table-column
              label="开始时间"
              prop="start_time"
            />
            <el-table-column
              label="结束时间"
              prop="end_time"
            />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="250"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-link
                  type="primary"
                  @click="
                    (addedFormVisible = true),
                      (type = 1),
                      (id = scope.row.id),
                      edit_assig(scope.$index)
                  "
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
    <!-- 增加和编辑 -->
    <el-dialog
      :title="type == 1 ? '编辑' : '新增'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog500"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="配送时间：" prop="createTime">
          <el-time-picker
            is-range
            v-model="info.createTime"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            placeholder="选择时间范围"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
          >
          </el-time-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  sendTime_index,
  sendTime_add,
  sendTime_edit,
  sendTime_delete,
} from "@/api/global/globalDelivery";
import rules from "@/rules/global/globalDelivery";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";

export default {
  mixins: [mixins],
  components: {},
  data() {
    return {
      // 增加和编辑
      info: {
        createTime: ["00:00:00", "00:00:00"],
        start_time: "",
        end_time: "",
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 验证
      rules,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    startGetting() {
      this.getArticleList();
    },
    // 关闭清空表单
    close() {
      this.info = {
        createTime: ["00:00:00", "00:00:00"],
        start_time: "",
        end_time: "",
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 搜索
    getArticleList() {
      this.sendTime_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          sendTime_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        createTime: [
          this.list.list.data[index].start_time,
          this.list.list.data[index].end_time,
        ],
        start_time: this.list.list.data[index].start_time,
        end_time: this.list.list.data[index].end_time,
      };
    },
    // 新增保存
    add() {
      let info = JSON.parse(JSON.stringify(this.info));
      info.start_time = info.createTime[0];
      info.end_time = info.createTime[1];
      let { createTime, ...form } = info;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form
      );
      sendTime_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.sendTime_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let info = JSON.parse(JSON.stringify(this.info));
      info.start_time = info.createTime[0];
      info.end_time = info.createTime[1];
      let { createTime, ...form } = info;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form,
        { id: this.id }
      );

      sendTime_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.sendTime_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 列表
    sendTime_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.current
      );
      sendTime_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.status = e.status == 1 ? true : false;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.getNumerical(120);
    this.sendTime_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 120px);
}

/deep/.el-date-editor.el-input__inner {
  width: 280px;
}
</style>
